package com.baosight.sczx.zm.ce.service;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.ce.WorkPlan;
import com.baosight.sczx.zm.ce.domain.ZMCEWorkRecord;

/**
 * <p>Title: ServiceZMCEV0.java</p>
 * <p>Description: 工作反馈</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 李红星
 * @version 1.0
 * <pre>Histroy:
 *          2012-2-21 13:22:44  李红星  Create
 *</pre>
 */
public class ServiceZMCEV0 extends ServiceEPBase {
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCEV0</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-2-21 13:23:54李红星
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.ce.department", "orgCode", "orgName", false));
		return outInfo;
	}

	/**
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCEV0</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-2-21 13:24:23 李红星
	 * </p>
	 *
	*/
	@Override
	public EiInfo query(EiInfo inInfo) {
//		StaticTools.setQueryParameters(inInfo, "fdStatus", "执行", true);
		StaticTools.setQueryParameters(inInfo, "fdDepartmentId", UserSession.getInSessionProperty("orgCode"), true);
		StaticTools.setQueryParameters(inInfo, "fdIsOrder", "否", true);
		EiInfo outInfo = super.query(inInfo, "ZMCEWorkRecord.query", new ZMCEWorkRecord());
		
		outInfo.set("fdDepartmentId", UserSession.getInSessionProperty("orgCode"));
		outInfo.set("loginDeptname", UserSession.getInSessionProperty("orgName"));
		outInfo.set("fdPersonId", UserSession.getUserId());
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.ce.department", "orgCode", "orgName", false));		
//		outInfo.addBlock(User.getInstance().getConstructionStaff("cmes.ce.people", "empCode", "empName", false));
		outInfo.addBlock(WorkPlan.getWorkPlanBlock("cmes.ce.workPlan", null));
		
		return outInfo;
	}
	/**
	 * 工作反馈新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCEV0</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-2-21 15:57:58 李红星
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		//校验名称、简称是否重复
		String more = inInfo.getBlock("result").getCellStr(0, "more");
		String fdDepartmentid = inInfo.getBlock("result").getCellStr(0, "fdDepartmentid");//执行部门
		String fdPersonId= inInfo.getBlock("result").getCellStr(0, "fdPersonId");//执行人
		String fdExceptionDescription = inInfo.getBlock("result").getCellStr(0, "fdExceptionDescription");
		String fdType = inInfo.getBlock("result").getCellStr(0, "fdType");
		String recordKey = TablePk.getInstance().getNextValue("T_CE_WORKRECORD");
		//		boolean validateValue = validate(inInfo, "ZMCEEquipmentDynamic.validate", new HashMap<String, String>());
//		
//		if(validateValue) {
//			return inInfo;
//		}
//		else {
			inInfo.getBlock("result").setCell(0, "fdKeyId", recordKey);//主键t_ce_workrecord
//			inInfo.getBlock("result").setCell(0, "fdPreparerId", UserSession.getUserId());
			inInfo.getBlock("result").setCell(0, "fdIsOrder", "否");//默认无单
//			inInfo.set("fdDepartmentId", UserSession.getInSessionProperty("orgCode"));
//			inInfo.getBlock("result").setCell(0, "fdPreparer", UserSession.getInSessionProperty("UserCname"));
//			inInfo.getBlock("result").setCell(0, "fdPreparerDepId", UserSession.getInSessionProperty("orgCode"));
			EiInfo outinfo = super.insert(inInfo, "ZMCEWorkRecord.insert", "result");
			//改变设备的使用状态
//			outinfo.set(EiConstant.serviceName, "ZMCE31");
//			outinfo.set(EiConstant.methodName, "changeType");
//			outinfo = SoaManager.invoke(outinfo);
			//同时保存异常情况进度设备异常记录
			if("1".equals(more)){//勾选汇报异常设备
				inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CE_UNUSUALPROBLEM"));
				
				if("润滑".equals(fdType)){
					inInfo.getBlock("result").setCell(0, "fdQuestionSource", "设备润换");
				}else if("保养".equals(fdType)){
					inInfo.getBlock("result").setCell(0, "fdQuestionSource", "设备保养");
				}else if("点检".equals(fdType)){
					inInfo.getBlock("result").setCell(0, "fdQuestionSource", "设备点检");
				}else if("大修".equals(fdType)){
					inInfo.getBlock("result").setCell(0, "fdQuestionSource", "设备大修");
				}else if("维修".equals(fdType)){
					inInfo.getBlock("result").setCell(0, "fdQuestionSource", "设备维修");
				}
				
				inInfo.getBlock("result").setCell(0, "fdWorkrecordId", recordKey);
				inInfo.getBlock("result").setCell(0, "fdUseDepartment", fdDepartmentid);
				inInfo.getBlock("result").setCell(0, "fdUseDepartment", fdPersonId);
				inInfo.getBlock("result").setCell(0, "fdDescription", fdExceptionDescription);
				inInfo.getBlock("result").setCell(0, "fdStatus", "未处理");
				inInfo.getBlock("result").setCell(0, "fdPerson", UserSession.getInSessionProperty("UserCname"));
				inInfo.getBlock("result").setCell(0, "fdDatetime", StaticTools.getDateTime());
				super.insert(inInfo, "ZMCEUnusualProblem.insert");
			}
			return outinfo;
//		}
	}
	
	/**
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCEV0</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-2-21 16:03:40 李红星
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo outInfo = super.update(inInfo, "ZMCEWorkRecord.update");
		return outInfo;
		
	}
	

	/**
	 * 设备变动删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCEV0</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-2-21 16:04:41 李红星
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo _temp = super.delete(inInfo, "ZMCEWorkRecord.delete");
		EiInfo outInfo = query(inInfo);
	    outInfo.setMsg(_temp.getMsg());
	    outInfo.setDetailMsg(_temp.getDetailMsg());
		
		return outInfo;
	}

}
